package net.lzzy.practices.service;

import net.lzzy.practices.constants.Messages;
import net.lzzy.practices.entities.Chapter;
import net.lzzy.practices.entities.Course;
import net.lzzy.practices.entities.User;
import net.lzzy.practices.repositories.ChapterRepository;
import net.lzzy.practices.repositories.CourseRepository;
import net.lzzy.practices.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Service
public class ChapterServiceImpl implements ChapterService {
    private final ChapterRepository chRepository;
    private final CourseRepository coRepository;


    public ChapterServiceImpl(ChapterRepository chRepository, CourseRepository coRepository, JwtUtil jwtUtil) {
        this.chRepository = chRepository;
        this.coRepository = coRepository;

    }


    @Override
    public List<Chapter> getTeachersChapters(UUID courseId, String token) {
        Course course = coRepository.getById(courseId);
        UUID teacherId = course.getTeacher().getId();
        if (!teacherId.toString().equals(JwtUtil.verifyToken(token))){
            return null;
        }
        return chRepository.findChaptersByCourseIdOrderByUpdateTime(courseId);
    }

    @Override
    public List<Chapter> getStudentsChapters(UUID studentId, UUID courseId, String token) {
        if (!studentId.toString().equals(JwtUtil.verifyToken(token))){
            return null;
        }
        return chRepository.findChaptersByCourseIdAndOpenOrderByUpdateTimeDesc(courseId,true);
    }

    @Override
    public Chapter getById(UUID id) {
        return chRepository.findById(id).orElse(null);
    }

    @Override
    public Chapter add(Chapter chapter) {
        return chRepository.save(chapter);
    }

    @Override
    public Chapter update(Chapter chapter, String token) {
        UUID teacherId = chapter.getCourse().getTeacher().getId();
        if (!teacherId.toString().equals(JwtUtil.verifyToken(token))){
            return null;
        }
        chapter.setUpdateTime(new Date());
        return chRepository.save(chapter);
    }

    @Override
    public Chapter openCloseChapter(boolean open, UUID id, String token) {
        Chapter chapter = chRepository.findById(id).orElse(null);
        if (chapter != null) {
            chapter.setOpen(open);
            return update(chapter,token);
        }
        return null;
    }

    @Override
    public Messages remove(UUID id, UUID courseId, String token) {
        Chapter chapter = chRepository.findById(id).orElse(null);
        if (chapter == null){
            return Messages.WRONG_ID;
        }
        Course course = coRepository.getById(courseId);
        UUID teacherId = course.getTeacher().getId();
        if (!teacherId.toString().equals(JwtUtil.verifyToken(token))){
            return Messages.NO_PERMISSION;
        }
        if (!chapter.getQuestions().isEmpty()){
            return Messages.RELATED_DATA;
        }
        chRepository.delete(chapter);
        return Messages.SUCCESS;
    }
}
